package com.generalbackend.role.controller;

import com.generalbackend.common.annotion.ApiPermission;
import com.generalbackend.common.enums.MenuEnum;
import com.generalbackend.common.model.CommonResult;
import com.generalbackend.common.model.entity.User;
import com.generalbackend.role.model.query.UserQuery;
import com.generalbackend.role.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
* @Description 用户表控制层
* @author tim.xie
* @date 2021-12-16 13:18
*/
@RestController
@RequestMapping(value = "/api/user")
public class UserController {

    @Autowired
    private IUserService  userService;


    @ApiPermission(name = "",menu = {MenuEnum.UserManage})
    @ApiOperation(httpMethod = "POST" ,value = "用户列表")
    @RequestMapping(value = "/list" ,method = RequestMethod.POST)
    public CommonResult list(@RequestBody UserQuery userQuery ){
           return  CommonResult.successCommonResult(userService.list(userQuery),"查询列表成功");
    }

    @ApiPermission(name = "",menu = {MenuEnum.UserManage})
    @ApiOperation(httpMethod = "POST",value = "用户添加")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public CommonResult add(@RequestBody User user){
        userService.insert(user);
        return CommonResult.successCommonResult("新增用户表成功");
    }

    @ApiPermission(name = "",menu = {MenuEnum.UserManage})
    @ApiOperation(httpMethod = "POST",value = "用户修改")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public CommonResult update(@RequestBody User user){
        userService.update(user);
        return CommonResult.successCommonResult("修改用户表成功");
    }

    @ApiPermission(name = "",menu = {MenuEnum.UserManage})
    @ApiOperation(httpMethod ="DELETE", value = "用户删除")
    @RequestMapping(value = "/deleteById",method = RequestMethod.DELETE)
    public CommonResult deleteById(@RequestParam("id")String id){
        userService.deleteById(id);
        return CommonResult.successCommonResult("删除用户表成功");
    }

    @ApiPermission(name = "",menu = {MenuEnum.UserManage})
    @ApiOperation(httpMethod = "POST",value = "获取用户")
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public CommonResult getById( @RequestParam("id")String id ){
        return CommonResult.successCommonResult(userService.selectById(id),"查询成功");
    }

    @ApiPermission(name = "",menu = {MenuEnum.UserManage})
    @ApiOperation(httpMethod = "POST",value = "重置用户密码")
    @RequestMapping(value = "/resetPassword",method = RequestMethod.POST)
    public CommonResult resetPassword(@RequestParam("id")String id ){
        if (!userService.resetPassword(Long.parseLong(id))) {
            return CommonResult.errorCommonResult("重置用户密码失败");
        }
        return CommonResult.successCommonResult("重置用户密码成功");
    }


}
